﻿using LuShanTea.Admin.Web.App_Start;
using LuShanTea.Admin.Web.Models;
using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class OrderRecordController : Controller
    {
        public IOrderRecordService OrderRecordService { get; set; }

        public IOrderService OrderService { get; set; }

        public IPackService PackService { get; set; }


        [CheckPermission("OrderRecord.List")]
        // GET: OrderRecord
        public ActionResult Index(long? id)
        {
            if (id == null)
            {
                return Content("参数错误");
            }

            var order = OrderService.GetById((long)id);

            if (order == null)
            {
                return Content("订单ID为" + id + "不存在");
            }

            var orderRecords = OrderRecordService.GetOrderRecordByOrderId((long)id);

            return View(orderRecords);
        }

        [CheckPermission("OrderRecord.Edit")]
        public ActionResult Edit(long id)
        {
            var orderRecord = OrderRecordService.GetById(id);

            if (orderRecord == null)
            {
                return Content("ID参数错误");
            }

            var packs = PackService.GetAll();
            OrderRecordEditGetModel model = new OrderRecordEditGetModel();
            model.OrderRecord = orderRecord;
            model.Packs = packs;

            return View(model);
        }

        [CheckPermission("OrderRecord.Edit")]
        [HttpPost]
        public ActionResult Edit(OrderRecordDTO orderRecordDTO)
        {
            var orderRecord = OrderRecordService.GetById(orderRecordDTO.Id);

            if (orderRecord == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "订单记录Id为:" + orderRecordDTO.Id + "不存在" });
            }

            var pack = PackService.GetById(orderRecordDTO.PackId);

            if (pack == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "包装Id为:" + orderRecordDTO.PackId + "不存在" });
            }

            orderRecord.Count = orderRecordDTO.Count;
            orderRecord.Price = orderRecordDTO.Price;
            orderRecord.PackId = orderRecordDTO.PackId;

            OrderRecordService.UpdateOrderRecord(orderRecord);


            return Json(new AjaxResult { Status = "ok" });
        }

        [CheckPermission("OrderRecord.Delete")]
        [HttpPost]
        public ActionResult Delete(long orderRecordId)
        {
            OrderRecordService.MarkDeleted(orderRecordId);

            return Json(new AjaxResult { Status = "ok" });
        }
    }
}